#include "work_queue.h"
#include "def.h"

typedef struct my_work {
	work_t work;
	int num;
}my_work_t;

static void my_work_func(void *arg)
{
	my_work_t *p_work = (my_work_t *)arg;
	printf("hello, %d\n", p_work->num);
	sleep(2);
	work_term(&p_work->work);
	safe_free(p_work);
}

int worker_test(void)
{
	printf("** worker test:\n");
	worker_t *self = worker_new();
	assert(self);

	int i = 0;
	while (i < 15) {
		++i;
		my_work_t *p_work = (my_work_t *)MALLOC(sizeof(my_work_t));
		assert(p_work);
		p_work->num = i;
		work_init(&p_work->work, p_work, my_work_func);

		worker_add_tail(self, &p_work->work);
		sleep(1);
	}

	printf("waiting...\n");
	while (worker_nums(self)) {
		sleep(2);
	}
	printf("destroy worker now>>>>>>>\n");
	worker_destroy(&self);
	printf("** worker test success\n");
	return 0;
}
